class RecipeController < ApplicationController
  protect_from_forgery :only => [:destroy]
  # GET /users
  # GET /users.xml
  def index
    @recipemedia = Media.find_all_by_rmedia_type("Recipe")
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @recipemedia }
    end
  end

  def add
  end

  def view
    @no_edit = true;
    @recipemedia = Media.find(params[:id])
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @recipemedia }
    end
  end

  def update
     @recipemedia = Media.find(params[:id])
     session[:current_recipe] = @recipemedia.rmedia
  end

  def update_finalize
        @old_recipe = Recipe.find(params[:id])

    respond_to do |format|
      if @old_recipe.update_attributes(params[:recipe])
        flash[:notice] = 'recipe was successfully updated.'
        format.html { redirect_to(:action => "view", :id=>@old_recipe) }
        format.xml  { head :ok }
      else
        format.html { render :action => "update" }
        format.xml  { render :xml => @cookbook.errors, :status => :unprocessable_entity }
      end
    end
  end

  # GET /recipe/new
  # GET /recipe/new.xml
  def new
   #@media = Media.new
   #@media.created_at = Time.now
   #@media.created_by = 1
   #@media.rmedia = Recipe.new
   @recipe = Recipe.new()
   @current_ingredient = @recipe.add_new_ingredient()
   session[:current_recipe] = @recipe
   respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @recipe }
    end
  end

  def get_current_recipe
     session[:current_recipe] ||= Recipe.new
  end

  def recipe_add_ingredient
    @recipe = get_current_recipe
    @current_ingredient = @recipe.add_new_ingredient()
    session[:current_recipe] = @recipe
    respond_to do |format|
      format.js
    end
  end

  def edit_ingredient
    @recipe =  get_current_recipe

    @current_ingredient =  @recipe.ingredient[params[:id].to_i]
    respond_to do |format|
      format.js
    end
  end

  def cancel_edit_ingredient
    @current_ingredient =  nil
    respond_to do |format|
      format.js
    end
  end

  def destroy_current_ingredient
    @recipe = get_current_recipe

    @recipe.ingredient.delete_at(params[:id].to_i)

    respond_to do |format|
      format.js
    end
  end

  def finalize_ingredient
    @recipe = get_current_recipe

    @recipe.ingredient[params[:id].to_i].name = params[:name]
    @recipe.ingredient[params[:id].to_i].amount = params[:amount]
    @recipe.ingredient[params[:id].to_i].amount_unit = params[:amount_unit]
    @recipe.ingredient[params[:id].to_i].notes = params[:notes]

    @current_ingredient = nil;

    respond_to do |format|
      format.js
    end
  end
   # POST /users
  # POST /users.xml
  def create
    # get the recipe object from the form
    @recipe = Recipe.new(params[:recipe])
    @temp_recipe = get_current_recipe
    @temp_recipe.ingredient.each { |e|
        @recipe.ingredient << e
      }
     respond_to do |format|
      unless @recipe.valid?
        format.html { render :action => "new" }
        format.xml  { render :xml => @media.rmedia.errors, :status => :unprocessable_entity }
      else
        # create a new media object
        @media = Media.new(:created_user => session[:user_name], :created_by => session[:user_id], :created_at => Time.now)
        @media.rmedia = @recipe
        if @media.save
          flash[:notice] = 'Recipe was successfully created.'
          format.html {  redirect_to(:controller => "recipe", :action => "index") }
          format.xml  { render :xml => @recipe, :status => :created, :location => @recipe }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @recipe.errors, :status => :unprocessable_entity }
        end
      end
    end
  end


  # DELETE /ingredients/1
  # DELETE /ingredients/1.xml
  def destroy
    @media = Media.find(params[:id])
    @media.destroy

    respond_to do |format|
      format.html { redirect_to(:controller => "recipe", :action => "index") }
      format.xml  { head :ok }
    end
  end

  def testnew
 
    @media = Media.new(:created_by => 1, :created_at => Time.now)
    @media.rmedia = Recipe.new(:title => "Cilantro Lime Rice", :category => "Rice",
                         :directions => "Mix all ingredients cook untill done", :description => "Cilantro lime rice is very good")
    @ing1 = Ingredient.new(:name => "Rice", :amount => 1, :amount_unit=>"cups", :notes=>"uncooked")
    @ing2 = Ingredient.new(:name => "Chicken Stock", :amount => 1, :amount_unit=>"cups")
    @ing3 = Ingredient.new(:name => "Cilantro", :amount => 0.5, :amount_unit=>"cups")
    @ing4 = Ingredient.new(:name => "Lime Juice", :amount => 2, :amount_unit=>"Table spoons")

    @media.rmedia.ingredient << @ing1
    @media.rmedia.ingredient << @ing2
    @media.rmedia.ingredient << @ing3
    @media.rmedia.ingredient << @ing4

    @media.save
    
    redirect_to(:controller => "recipe", :action => "index")
  end











  def create_comment
     @new_comment = Comment.new(params[:comment])
     @new_comment.user_id = session[:user_id]
     @recipe_id = @new_comment.recipe_id
    respond_to do |format|
      if @new_comment.save
         format.js
      else
       # do nothing
      end
    end
  end

  def create_rating
    @rating = Rating.new(params[:rating])
     @recipe_id = @rating.recipe_id
    respond_to do |format|
      if @rating.save
         format.js
      else
       # do nothing
      end
    end
  end
     
  def create_cookbook_recipe
    @new_cookbook_recipe = CookbookRecipe.new(params[:cookbook_recipe])
    @recipe_id = @new_cookbook_recipe.recipe_id
    respond_to do |format|
      if @new_cookbook_recipe.valid?
         @new_cookbook_recipe.save
         format.js
      else
       # do nothing
      end
    end
  end
  




  def edit_recipe_edit_ingredient
    @current_ingredient = Ingredient.find(params[:id])
    @recipe = Recipe.find(get_current_recipe)
    respond_to do |format|
      format.js
    end
  end

  def edit_add_new_ingredient
    @recipe = Recipe.find(get_current_recipe)
    @ing = Ingredient.new
    @recipe.ingredient << @ing
    @recipe.save
    @current_ingredient = @ing
    respond_to do |format|
      format.js
    end
  end

  def edit_finalize_ingredient
    @current_ingredient = nil

    @ing = Ingredient.find(params[:id])
    @ing.update_attributes({:name => params[:name],
                      :amount => params[:amount],
                      :amount_unit => params[:amount_unit],
                      :notes => params[:notes]})
    @recipe = Recipe.find(@ing.recipe_id)

    respond_to do |format|
      format.js
    end
  end

  def edit_cancel_edit_ingredient
    @current_ingredient = nil
    @ing = Ingredient.find(params[:id])
    @recipe = Recipe.find(@ing.recipe_id)
    respond_to do |format|
      format.js
    end
  end

  def edit_destroy_current_ingredient
    @current_ingredient = nil
    @ing = Ingredient.find(params[:id])
    @recipe = Recipe.find(@ing.recipe_id)
    @ing = Ingredient.find(params[:id])
    @ing.destroy
    respond_to do |format|
      format.js
    end
  end

  def search
  @search_text = params[:search_text]
  if @search_text.nil?
    @search_text = ""
  end
 @results = Recipe.find(:all, :conditions => ['title LIKE ? or category LIKE ? or description LIKE ?',
                                                 '%'+@search_text+'%',
                                                 '%'+@search_text+'%',
                                                 '%'+@search_text+'%'])
   respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @recipemedia }
    end
  end

  def search_ajax
    @search_text = params[:search_text]
    if @search_text.nil?
      @search_text = ""
    end
  @results = Recipe.find(:all, :conditions => ['title LIKE ? or category LIKE ? or description LIKE ?',
                                                 '%'+@search_text+'%',
                                                 '%'+@search_text+'%',
                                                 '%'+@search_text+'%'])
    respond_to do |format|
      format.js
    end
  end
end
